package com.saturn.titan.core.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.saturn.titan.core.bean.PlatformEmp;
import com.saturn.titan.core.bean.PlatformEmpRole;
import com.saturn.titan.core.comm.CRMCache;
import com.saturn.titan.core.repository.mybatis.PlatformEmpDao;
import com.saturn.titan.core.repository.mybatis.PlatformEmpRoleDao;

/**
 * 员工服务类
 * @author Administrator
 *
 */
@Component
@Transactional(readOnly = true)
public class EmpService {

	@Autowired
	private PlatformEmpDao platformEmpDao;
	@Autowired
	private PlatformEmpRoleDao platformEmpRoleDao;
	
	public List<PlatformEmp> findAllEmp(){
		return platformEmpDao.select(new PlatformEmp());
	}

	@Transactional(readOnly = false)
	public void deleteById(Long id) {
		platformEmpDao.deleteById(id);
		CRMCache.EMP_CACHE.remove(id);
	}

	@Transactional(readOnly = false)
	public void add(PlatformEmp a,List<Long> roleIds) {
		platformEmpDao.insert(a);
		PlatformEmpRole tmp = null;
		if(!roleIds.isEmpty()){
			for(Long id : roleIds){
				tmp = new PlatformEmpRole();
				tmp.setEmpId(a.getId());
				tmp.setRoleId(id);
				platformEmpRoleDao.insert(tmp);
			}
			
		}
		CRMCache.EMP_CACHE.put(a.getId(),a);
	}
	
	@Transactional(readOnly = false)
	public void add(PlatformEmp a) {
		platformEmpDao.insert(a);
		CRMCache.EMP_CACHE.put(a.getId(),a);
	}

	@Transactional(readOnly = false)
	public void deleteNode(Long id) {
		platformEmpDao.deleteById(id);
		CRMCache.EMP_CACHE.remove(id);
	}

	@Transactional(readOnly = false)
	public void update(PlatformEmp a) {
		platformEmpDao.update(a);
		CRMCache.EMP_CACHE.put(a.getId(),platformEmpDao.selectById(a.getId()));
	}
	
	@Transactional(readOnly = false)
	public void update(PlatformEmp a,List<Long> roleIds) {
		platformEmpDao.update(a);
		if(roleIds != null){
			PlatformEmpRole tmp = null;
			tmp = new PlatformEmpRole();
			tmp.setEmpId(a.getId());
			platformEmpRoleDao.delete(tmp);
			if(!roleIds.isEmpty()){
				for(Long id : roleIds){
					tmp = new PlatformEmpRole();
					tmp.setEmpId(a.getId());
					tmp.setRoleId(id);
					platformEmpRoleDao.insert(tmp);
				}
			}
		}
		CRMCache.EMP_CACHE.put(a.getId(),platformEmpDao.selectById(a.getId()));
	}

	public List<PlatformEmp> findEmp(PlatformEmp ba) {
		return platformEmpDao.select(ba);
	}
	public List<PlatformEmp> getEmpByAreaAndRole(Long areaId){
		return platformEmpDao.getEmpByAreaAndRole(areaId);
	}
}
